package com.bwie.service.Impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.Type;
import com.bwie.mapper.TypeMapper;
import com.bwie.service.TypeService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@Service
@Transactional
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements TypeService{
    @Autowired
    private TypeMapper typeMapper;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private FastFileStorageClient fileStorageClient;
    @Override
    public R getTypeList(PageInfoVo pageInfoVo) {
        List<Type> typeList = typeMapper.ghetTypeList(pageInfoVo);
        List<Tree<Integer>> build = TreeUtil.build(typeList, 0, (tree, treeNode) -> {
            treeNode.setId(tree.getTypeId());
            treeNode.setName(tree.getTypeName());
            treeNode.setParentId(tree.getTypePid());
            treeNode.putExtra("typeTop", tree.getTypeTop());
            treeNode.putExtra("typeStatus", tree.getTypeStatus());
            treeNode.putExtra("typeImage", tree.getTypeImage());
        });
        return R.OK(build);
    }

    @Override
    public R addTypeList(Type type) {
        Type type1 = typeMapper.selectOne(
                new QueryWrapper<Type>().lambda().eq(Type::getTypeName, type.getTypeName())
        );
        if(ObjectUtils.isNotEmpty(type1)){
            return R.ERROR(500,"类型已存在，请重新输入");
        }
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return R.ERROR(500,"请先登录");
        }
        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        String userName = body.get("userName", String.class);
        type.setCreateBy(userName);
        type.setCreateTime(new Date());
        type.setUpdateBy(userName);
        type.setUpdateTime(new Date());
        typeMapper.insert(type);
        return R.OK();
    }

    @Override
    public R uploadFile(MultipartFile file) {
        try {
            StorePath storePath = fileStorageClient.uploadFile(file.getInputStream(), file.getSize(), FileUtil.extName(file.getOriginalFilename()), null);
            String url = "http://192.168.80.192:8888/"+storePath.getFullPath();
            return R.OK(url);
        } catch (IOException e) {
            e.printStackTrace();
            return R.ERROR(500,"文件上传失败");
        }
    }

    @Override
    public R getSelectTypeListTree() {
        List<Type> list = this.list();
        List<Tree<Integer>> build = TreeUtil.build(list, 0, (tree, treeNode) -> {
            treeNode.setId(tree.getTypeId());
            treeNode.setName(tree.getTypeName());
            treeNode.setParentId(tree.getTypePid());
            treeNode.putExtra("value", tree.getTypeId());
            treeNode.putExtra("label", tree.getTypeName());
        });
        return R.OK(build);
    }
}
